const app = getApp()
Page({
    data: {
        config: app.config,

        banner:[],
        active:[],
        zoneImg:[],
        
        img:'https://api.lezuxia.cn/imgage/ff59c1007a5f482795967b0da951d53e.png',
        icon:[
            {title:'国行正品',icon:'icon-zhengpin'},
            {title:'芝麻信用',icon:'icon-zhima1'},
            {title:'隐私安全',icon:'icon-anquan'},
            {title:'0元免押',icon:'icon-jine'},
        ],
        prudects: [],


        // 专区
        detail:{
            tabs: [
                { title: '苹果专区', subTitle: '苹果' },
                { title: '华为专区', subTitle: '华为' },
                { title: '其他专区', subTitle: '' },
            ],
        },
        activeTab:0,

        goodsList: [],
        activePop: -1,
        popCssSta: 1,
        popCssStaMax: 1,

        topBannarColor: 0,
        titleBarHeight: 80,
    },
    
    onLoad(option) {
        app._init_(this)
        this.setBannarHei();
        this.getHome();
    },

    setBannarHei(){
      my.getSystemInfo({
        success: (res) => {
          let heigh = res.statusBarHeight + res.titleBarHeight;
          this.setData({
            titleBarHeight: heigh
          })
        }
      })
    },

    onPageScroll({ scrollTop }) {
      let ratio = Math.floor(scrollTop/6);
      let opention = ratio*0.05;
      this.setData({
        topBannarColor: opention
      })
    },


    // 弹窗跳转
    goActivePage(e){
      this.closeIdPop();
      app.navigateTo(e)
    },
    // 关闭弹窗
    closeIdPop(){
      this.setData({
        popCssStaMax: 2
      })
      setTimeout(()=>{
        this.setData({
          popCssSta: 2
        })
      },300)
    },


    // tabs
    handleTabClick({ index, tabsName }){
        this.setData({
            [tabsName]: index,
        });
    },


    getHome(){
        app.other.home()
        .then(res=>{
            const data = JSON.parse(res.data.slideShows).data

            data.zone.forEach(item=>{
              this.getminPriceSplit(item.newProduct);
            })

            this.setData(data)
            let eventsGoods = JSON.parse(res.data.eventsGoods)
            let prudects =eventsGoods.prudects;
			console.log(prudects)
            this.getminPriceSplit(prudects);
            
            this.setData({prudects});

            if(data.activeNew){
              let sta = -1;
              data.activeNew.forEach((item,index)=>{
                if(item.isShow){
                  sta = index;
                }
              })
              this.setData({
                activePop: sta
              });
            }
        })
        let obj = {
          refresh: 'false'
        }
        app.other.simpleProduct(obj)
        .then(res=>{
            if(res.code == 200 && res.data){
              let arr = [];
              let arr1 = res.data.apple;
              let arr2 = res.data.huawei;
              let arr3 = res.data.xiaomi;
              
              setTimeout(()=>{
                this.dataPromise(arr1);
                arr.push(arr1);
              },300)
              setTimeout(()=>{
                this.dataPromise(arr2);
                arr.push(arr2);
              },300)
              setTimeout(()=>{
                this.dataPromise(arr3);
                arr.push(arr3);
              },300)
              setTimeout(()=>{
                this.setData({
                goodsList: arr
              })
              },500)

            }
            
        })
    },

     // 转价格
    dataPromise(list){
        return new Promise((relose, reject)=>{
            list.forEach((item) => {
                item.params = JSON.parse(item.params)
                item.slideshows = JSON.parse(item.slideshows)
                this.getminPrice(item.params)
                    .then(res => {
                        // item.price = res;
                        let aa = res + '';
                        item.priceFront = aa.split('.')[0];
                        item.priceLater = aa.split('.')[1];
                    })
            })
            relose(list)
        })
    },

    getminPrice(params){
        return new Promise((resole,reject)=>{
            let prices=[]
            for (let i = 0;i<params.length;i++){
                for (let t = 0; t < params[i].term_list.length; t++) {
                    for (let s = 0; s < params[i].term_list[t].specifications.length; s++) {
                        let price = (params[i].term_list[t].specifications[s].money / params[i].term_list[t].value / 100).toFixed(2)
						if (params[i].term_list[t].value == 14 || params[i].term_list[t].value == 24) {
							 price = (params[i].term_list[t].specifications[s].money / (params[i].term_list[t].value  * 7 )/ 100).toFixed(2)
						} 
                        prices.push(price)
                    }
                }
            }
            let minprice = Math.min.apply(null, prices);
            resole(minprice)
        })
    },


    getminPriceSplit(params){
        return new Promise((resole,reject)=>{
            let prices=[]
            for (let i = 0;i<params.length;i++){
              let num = params[i].price + '';
              params[i].priceFront = num.split('.')[0]
              params[i].priceLater = num.split('.')[1]
            }
            resole(params)
        })
    },


    // 跳转
    navigateTo(e){
        app.navigateTo(e)
    },
    navigateToBtn(e){
      let con = e.target.dataset
      let url = con.url + '?param=' + con.param + '&&goFun=' + con.goFun
      my.navigateTo({
        url: url,
      })
    },

    // 
    definedGoPage(e){
      let item = e.target.dataset.item;
      console.log(999,item);
      if(item.router){
        my.navigateTo({
          url: item.router + '?param=' + item.id
        })
      }else{
        my.navigateToMiniProgram({
          appId: item.outAppid,
          path: item.outRouter + '?param=' + item.id,
          success: (res) => {
            // console.log(111,JSON.stringify(res))
          },
          fail: (res) => {
            // console.log(222,JSON.stringify(res))
          },
          complete:(res) => {
              // console.log(333,JSON.stringify(res))
          }
        });
      }
    },

    // 跳对应分类
    goPageFenLei(e){
      let con = e.target.dataset;
      app.goPageNum = con.param;
      app.categorySta = true;
      app.navigateTo(e);
    },

    // 更多
    goPage(val){
      let con = val.target.dataset.val[0].router;

      my.navigateTo({
        url: `/pages/active/specially/specially?type=${con}`
      })
    },
});
